package com.njxxted.activiti.modules.process.identity.controller;

import com.njxxted.activiti.common.response.ResponseVo;
import com.njxxted.activiti.common.util.StringUtils;
import com.njxxted.activiti.modules.base.controller.BaseController;
import com.njxxted.activiti.modules.process.identity.service.IActIdentityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.IdentityService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 描述：流程引擎 用户权限 controller
 * <p>
 * 作者：Ostrich Hu
 * 时间：2019/9/5 16:21 星期四
 */
@Api(tags = "流程权限管理")
@RestController
@RequestMapping(value = "/act/rest/identity")
public class ActIdentityController extends BaseController {

    private final IdentityService identityService;

    private final IActIdentityService iActIdentityService;

    public ActIdentityController(IdentityService identityService, IActIdentityService iActIdentityService) {
        this.identityService = identityService;
        this.iActIdentityService = iActIdentityService;
    }

    @PostMapping(value = "/list/{type}")
    @ApiOperation(value = "查询用户,部门,角色", notes = "查询用户,部门,角色")
    @ApiImplicitParam(value = "表单类型", name = "type", example = "depart 或 role 或 user")
    public ResponseVo getSelectTree(@PathVariable String type) {
        if ("role".equals(type)) {
            List<Map<String, Object>> select = iActIdentityService.getSelect(null, type);
            return ResponseVo.ok(select);
        } else {
            List<Map<String, Object>> selectTree = iActIdentityService.getSelectTree(type);
            return ResponseVo.ok(selectTree);
        }
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "查询所有用户")
    public ResponseVo getActAllUser(String param) {
        String appendWhere = null;
        if (StringUtils.isNotEmpty(param)) {
            StringBuilder builder = new StringBuilder();
            builder.append("(iu.First_ like '%").append(param).append("%' or ").append("iu.LAST_ like '%").append(param).append("%')");
            appendWhere = builder.toString();
        }
        List<Map<String, Object>> userByGroupId = iActIdentityService.getUserByGroupId(null, appendWhere);
        return ResponseVo.ok(userByGroupId);
    }
}
